//
//  HTTPTool.m
//  AFNetworkingDemo
//
//  Created by yang on 15/3/23.
//  Copyright (c) 2015年 yangzhiqiang. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"

@implementation HTTPTool


+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params  isToken:(BOOL) isToken success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    if ([ValidReachabilityFlags isConnectionAvailable]) {
        // 1.创建请求管理对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        if(isToken) {
            NSString * token = [NSKeyedUnarchiver unarchiveObjectWithFile:LoginToken] ;
            [manager.requestSerializer setValue:token forHTTPHeaderField:@"authentication"];
        }
        
        // 2.发送请求
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
    } else {
        [[HUDView shardCMNHUDView] showTitle:@"请检查网络"];
    }
}


+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params  isToken:(BOOL) isToken success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    if ([ValidReachabilityFlags isConnectionAvailable]) {
        // 1.创建请求管理对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        if(isToken) {
            NSString * token = [NSKeyedUnarchiver unarchiveObjectWithFile:LoginToken] ;
            [manager.requestSerializer setValue:token forHTTPHeaderField:@"authentication"];
        }
        
        // 2.发送请求
        [manager POST:url parameters:params
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  if (success) {
                      success(responseObject);
                  }
              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  if (failure) {
                      failure(error);
                  }
              }];
    } else {
        [[HUDView shardCMNHUDView] showTitle:@"请检查网络"];
    }
}

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params  isToken:(BOOL) isToken formDataArray:(NSArray *)formDataArray success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    if ([ValidReachabilityFlags isConnectionAvailable]) {
        // 1.创建请求管理对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        if(isToken) {
            NSString * token = [NSKeyedUnarchiver unarchiveObjectWithFile:LoginToken] ;
            [manager.requestSerializer setValue:token forHTTPHeaderField:@"authentication"];
        }
        
        // 2.发送请求
        [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> totalFormData) {
            for (GossipFormData *formData in formDataArray) {
                [totalFormData appendPartWithFileData:formData.data name:formData.name fileName:formData.filename mimeType:formData.mimeType];
            }
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
    } else {
        [[HUDView shardCMNHUDView] showTitle:@"请检查网络"];
    }
}

@end


@implementation GossipFormData


@end

